import json


from flask_restplus import Model, fields

# CommonShare
CommonShare = Model('CommonShare', {
    'title': fields.String(required=True, description='分享标题'),
    'img': fields.String(required=True, description='分享图片地址'),
    'describe': fields.String(required=True, description='分享描述'),
    'url': fields.String(required=True, description='分享跳转链接'),
})

# CommonUser
CommonUser = Model('CommonUser', {
    'id': fields.String(required=True, description='用户id', default=0),
    'icon': fields.String(required=True, description='用户icon地址', default=0),
    'name': fields.String(required=True, description='用户名', default=0),
    'type': fields.String(required=True, description='用户类型', default=0),
    'authentication_name': fields.String(required=True, description='作者名字', default=0),
    'introduction': fields.String(required=True, description='简介', default=0),
    'follow': fields.Integer(required=True, description='简介', default=0),
})

BaseResp = Model('BaseResp', {
    'status': fields.Integer(description='成功与否', default=200),
    'message': fields.String(description='消息', default='成功'),
})


# JSON String
class JSONString(fields.Raw):
    def format(self, value):
        # 如果是string就json.loads
        if isinstance(value, str):
            return json.loads(value)
        elif isinstance(value, dict):
            return value
        else:
            raise TypeError('the value object is wrong!')


# 浮点数取整2
class FLOAT2(fields.Float):
    def format(self, value):
        return round(value, 2)

